<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    protected $table = 'Videos';
    const UPDATED_AT = null;
    // 自定义常量
    const STATUS_DRAFT = 0;
    const STATUS_PUBLISHED = 1;
    const STATUS_REMOVED = 2;

    // protected $fillable = [
    //     'club_id', 'admin_id', 'status', 'videos', 'title', 'intro', 'price', 'published_at'
    // ];
    protected $guarded = ['id','created_at'];  //fillable 反义词

    protected $dates = [
        'published_at'
    ];

    protected $casts = [
        'videos' => 'object',
    ];

    public function club()
    {
        return $this->belongsTo(Club::class);
    }

    public function admin()
    {
        return $this->belongsTo(Admin::class);
    }

    /**
     * 添加视频
     * @param object $video 要添加的视频对象
     * @return void
     */
    public function addVideos($videos)
    {
        if (is_null($videos))
            return;
        $this->attributes['videos'] = json_encode(array_merge($this->videos, $videos));
    }

    /**
     * 修改视频的可见性状态（active）
     * @param array $indexes 设为可见的视频的索引
     * @return void
     */
    public function setVideosActive($indexes)
    {
        if (is_null($indexes))
            $indexes = [];
        $obj = $this->videos;
        foreach ($obj as $index => $v) {
            $obj[$index]->active = in_array($index, $indexes);
        }
        $this->attributes['videos'] = json_encode($obj);
    }
}
